<!-- 人员 -->

<template>
  <FormSlot label="运送员" bind="resolverUserId" tip="请选择运送员" required>
    <div>
      <span v-if="one.resolverUserId" class="el-button--text" @click="toOpen">
        {{ one.resolverUserName }}{{ one.resolverUserPhone ? `(${one.resolverUserPhone})` : '' }}
      </span>

      <span v-if="!isShow">
        <BtnText v-if="one.resolverUserId" title="清除" @click="toClear" />
        <BtnPrimary v-else title="选择" @click="toOpen" />
      </span>
    </div>

    <EasyModal v-model="easyVisible" title="选择运送员" medium @submit="toSubmit" @close="toReset">
      <ChooseUserOne :obj="obj" bind="user" :grayed="grayed" />
    </EasyModal>
  </FormSlot>
</template>

<script>
// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'

const EMPTY = { uaaUserId: '', name: '', phone: '', organId: [], areaId: '', areaName: '' }

// ============================== 导出组件 ============================== //

export default {
  /**
   * 名称定义 (实际使用名称)
   */
  name: 'MyUser',

  /**
   * 混入列表 (配置选项合并)
   */
  mixins: [CORE.mixWeb(), CORE.mixSelect('all', 'all_')],

  /**
   * 状态数据 (函数返回对象)
   */
  data() {
    return {
      easyVisible: false, // 弹窗是否可见
      obj: { user: CORE.clone(EMPTY) }, // 数据对象
    }
  },

  /**
   * 计算属性 (慎用箭头函数)
   */
  computed: {
    /**
     * 禁用列表
     */
    grayed() {
      return this.all_select.map((item) => item.uaaUserId)
    },
  },

  /**
   * 监听属性 (深度 deep 首次 immediate)
   */
  watch: {
    /**
     * 监听弹窗可见
     */
    visible(visible) {
      visible && this.toReset()
    },
  },

  /**
   * 本地方法 (轻量重要在前)
   */
  methods: {
    /**
     * 弹窗
     */
    toOpen() {
      this.easyVisible = true
    },

    /**
     * 提交
     */
    toSubmit() {
      const { obj, one } = this
      one.resolverUserId = obj.user.uaaUserId
      one.resolverUserName = obj.user.name
      one.resolverUserPhone = obj.user.phone
      one.organizationId = obj.user.organId[0] || ''
      one.areaId = obj.user.areaId
      this.easyVisible = false
    },

    /**
     * 重置
     */
    toReset() {
      const {
        resolverUserId: uaaUserId,
        resolverUserName: name,
        resolverUserPhone: phone,
        organizationId,
        areaId,
      } = this.one
      this.obj = { user: { uaaUserId, name, phone, organId: [organizationId], areaId } }
    },

    /**
     * 清除
     */
    toClear() {
      this.one.resolverUserId = ''
      this.one.resolverUserName = ''
      this.one.resolverUserPhone = ''
      this.one.organizationId = ''
      this.one.areaId = ''
      this.obj = { user: CORE.clone(EMPTY) }
    },
  },
}
</script>
